package com.capacitydorm.server.web;

import com.capacitydorm.server.common.vo.DataGridBean;
import com.capacitydorm.server.service.VisitLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/visit")
public class VisitLogController {

    @Autowired
    private VisitLogService visitLogService;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @GetMapping
    public ResponseEntity<DataGridBean> getVisitLogs(Integer page, Integer rows) {
        try {
            DataGridBean result = visitLogService.findVisitVos(page, rows);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/updateStuSta")
    public ResponseEntity<Void> updateStuSta(String key) {
        try {
            kafkaTemplate.send("dormLog", "key", key);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
